package com.zmn.oms.services.impl.order;

import com.vdurmont.emoji.EmojiParser;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.model.entity.order.OrderExtend;
import com.zmn.oms.persistence.interfaces.order.OrderExtendDao;
import com.zmn.oms.services.interfaces.order.OrderExtendService;
import com.zmn.saas.common.utils.EmojiUtil;
import io.shardingsphere.api.HintManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 类描述：订单扩展实现类
 *
 * @author heciqi
 * @date 2018/12/04 15:28
 */
@Service
public class OrderExtendServiceImpl implements OrderExtendService {

    @Resource
    private OrderExtendDao extendDao;

    /**
     * 根据订单Id查询扩展数据，如果不需要outerData数据时，推荐使用findMiniOrderExtendByOrderId()方法
     *
     * @param orderId
     * @return
     */
    @Override
    public OrderExtend findOrderExtendByOrderId(Long orderId) {
        OrderExtend orderExtend = extendDao.findOrderExtendByKey(orderId);
        toUnicode(orderExtend);
        return orderExtend;
    }

    /**
     * 根据订单Id查询扩展数据，该查询返回的结果中不包含outerData字段中的数据
     *
     * @param orderId
     * @return
     */
    @Override
    public OrderExtend findMiniOrderExtendByOrderId(Long orderId) {
        OrderExtend orderExtend = extendDao.findMiniOrderExtendByKey(orderId);
        toUnicode(orderExtend);
        return orderExtend;
    }

    /**
     * 根据订单Id查询扩展数据，该查询返回的结果中不包含outerData字段中的数据
     *
     * @param orderId
     * @return
     */
    @Override
    public OrderExtend findMiniOrderExtendByOrderIdSrcMaster(Long orderId) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            OrderExtend orderExtend = extendDao.findMiniOrderExtendByKey(orderId);
            toUnicode(orderExtend);
            return orderExtend;
        }
    }

    /**
     * 根据订单Id查询扩展数据，该查询返回的结果中不包含outerData字段中的数据。
     * 该方法主要用于ES同步数据时，获取渠道第二（扩展）订单号
     *
     * @param orderIdList
     * @return
     */
    @Override
    public List<OrderExtend> listMiniOrderExtendByOrderIdList(List<Long> orderIdList) {
        List<OrderExtend> orderExtends = extendDao.listMiniOrderExtendByKeyList(orderIdList);
        orderExtends.forEach(e -> toUnicode(e));
        return orderExtends;
    }

    /**
     * 修改
     *
     * @param orderExtend
     * @return
     */
    @Override
    public int updateOrderExtend(OrderExtend orderExtend) {
        toAliases(orderExtend);
        boolean exists = extendDao.countByPrimaryKey(orderExtend.getExtId()) > 0;
        if (exists) {
            orderExtend.setUpdater(Optional.ofNullable(orderExtend.getUpdater()).orElse("系统"));
            return extendDao.updateByPrimaryKeySelective(orderExtend);
        } else {
            if (StringUtil.isBlank(orderExtend.getExtOuterId())) {
                orderExtend.setExtOuterId(null);
            }
            orderExtend.setCreater(Optional.ofNullable(orderExtend.getCreater()).orElse("系统"));
            return extendDao.insertSelective(orderExtend);
        }
    }

    @Override
    public void insertOrderExtend(OrderExtend orderExtend) {
        toAliases(orderExtend);
        orderExtend.setCreateTime(Optional.ofNullable(orderExtend.getCreateTime()).orElse(DateUtil.getNow()));
        orderExtend.setCreater(Optional.ofNullable(orderExtend.getCreater()).orElse("系统"));
        extendDao.insertSelective(orderExtend);
    }

    @Override
    public int updateBizClueId(OrderExtend orderExtend) {
        toAliases(orderExtend);
        orderExtend.setUpdateTime(Optional.ofNullable(orderExtend.getCreateTime()).orElse(DateUtil.getNow()));
        orderExtend.setUpdater(Optional.ofNullable(orderExtend.getCreater()).orElse("系统"));
        boolean exists = extendDao.countByPrimaryKey(orderExtend.getExtId()) > 0;
        if (exists) {
            return extendDao.updateBizClueId(orderExtend);
        } else {
            if (StringUtil.isBlank(orderExtend.getExtOuterId())) {
                orderExtend.setExtOuterId(null);
            }
            orderExtend.setCreater(Optional.ofNullable(orderExtend.getCreater()).orElse("系统"));
            return extendDao.insertSelective(orderExtend);
        }
    }

    /**
     * 清空贴花照片
     *
     * @param extId
     * @return
     */
    @Override
    public void clearAppliqueSrcByKey(Long extId) {
        extendDao.clearAppliqueSrcByKey(extId);
    }

    /**
     * 清空贴花照片和其它照片
     *
     * @param extId
     * @return
     */
    @Override
    public void clearAppliqueSrcAndImageSrcByKey(Long extId) {
        extendDao.clearAppliqueSrcAndImageSrcByKey(extId);
    }

    /**
     * 清空预付款凭证照片
     *
     * @param extId
     * @return
     */
    @Override
    public void clearChannelPrepayCertificateSrcByKey(Long extId) {
        boolean exists = extendDao.countByPrimaryKey(extId) > 0;
        if (exists) {
            extendDao.clearChannelPrepayCertificateSrcByKey(extId);
        }
    }

    /**
     * 修改
     *
     * @param orderExtend
     * @return
     */
    @Override
    public void clearDistributeInfo(OrderExtend orderExtend) {
        orderExtend.setCreateTime(Optional.ofNullable(orderExtend.getCreateTime()).orElse(DateUtil.getNow()));
        orderExtend.setCreater(Optional.ofNullable(orderExtend.getCreater()).orElse("系统"));

        boolean exists = extendDao.countByPrimaryKey(orderExtend.getExtId()) > 0;
        if (exists) {
            extendDao.clearDistributeInfoByKey(orderExtend);
        }
    }

    /**
     * 表情符号转换为字符
     * @param orderExtend
     */
    private void toAliases(OrderExtend orderExtend) {
        if (Objects.isNull(orderExtend)) {
            return;
        }
        String creater = orderExtend.getCreater();
        if (StringUtil.isNotBlank(creater)) {
            orderExtend.setCreater(EmojiUtil.filterEmoji(creater));
        }
    }

    /**
     * 字符转换为表情符号
     * @param orderExtend
     */
    private void toUnicode(OrderExtend orderExtend) {
        if (Objects.isNull(orderExtend)) {
            return;
        }
        String creater = orderExtend.getCreater();
        if (StringUtil.isNotBlank(creater)) {
            orderExtend.setCreater(EmojiParser.parseToUnicode(creater));
        }
    }

}
